package org.abacus.client.ui.view;

import org.abacus.client.config.layout.GridDataConfig;
import org.abacus.client.config.layout.GridLayoutConfig;
import org.abacus.client.config.widgets.ViewConfig;
import org.abacus.client.dao.config.widgets.ViewConfigDao;
import org.abacus.client.ui.access.AccessDataFactory;
import org.abacus.dao.DaoFactory;
import org.abacus.platform.core.util.GUID;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class ViewConfigHelper {
	private ViewConfigDao viewConfigDao = DaoFactory.getInstance(ViewConfigDao.class);
	private AccessDataFactory accessDataFactory = AccessDataFactory.getInstance();

	public static synchronized ViewConfigHelper getInstance() {
		return new ViewConfigHelper();
	}

	private ViewConfigHelper() {
		super();
	}

	public void createViewConfig(Composite composite, int parentPosition, ViewConfig parent) {
		GridLayoutConfig vc = accessDataFactory.getGridLayoutConfig((GridLayout) composite.getLayout());
		if (null != vc) {
			vc.setId(GUID.value());
			viewConfigDao.saveOrUpdate(vc);
		}
		GridDataConfig gdc = accessDataFactory.getGridDataConfig((GridData) composite.getLayoutData());
		if (null != gdc) {
			gdc.setId(GUID.value());
			viewConfigDao.saveOrUpdate(gdc);
		}
		ViewConfig viewConfig = accessDataFactory.getViewConfig(composite.getClass());
		if (null == viewConfig) {
			return;
		}
		viewConfig.setParent(parent);
		viewConfig.setPosition(parentPosition);
		viewConfig.setLayoutConfig(vc);
		viewConfig.setLayoutDataConfig(gdc);
		viewConfig.setStyle(composite.getStyle());
		if (composite instanceof Group) {
			viewConfig.setCode((((Group) composite).getText()));
		}
		if (composite instanceof Shell) {
			viewConfig.setCode((((Shell) composite).getText()));
		}
		viewConfigDao.saveOrUpdate(viewConfig);
		if (null != composite.getChildren()) {
			int position = 0;
			for (Control control : composite.getChildren()) {
				if (control instanceof Composite) {
					createViewConfig((Composite) control, position, viewConfig);
				} else {
					createViewConfig(control, position, viewConfig);
				}
				position++;
			}
		}
	}

	public void createViewConfig(Control control, int position, ViewConfig parent) {
		GridDataConfig gdc = accessDataFactory.getGridDataConfig((GridData) control.getLayoutData());
		if (null != gdc) {
			gdc.setId(GUID.value());
			viewConfigDao.saveOrUpdate(gdc);
		}
		ViewConfig viewConfig = accessDataFactory.getViewConfig(control.getClass());
		if (null == viewConfig) {
			return;
		}
		viewConfig.setParent(parent);
		viewConfig.setPosition(position);
		viewConfig.setLayoutDataConfig(gdc);
		viewConfig.setStyle(control.getStyle());
		if (control instanceof Label) {
			viewConfig.setCode((((Label) control).getText()));
		}
		if (control instanceof Button) {
			viewConfig.setCode((((Button) control).getText()));
		}
		viewConfigDao.saveOrUpdate(viewConfig);
	}
}
